package servlet;

import util.DBUtils;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.List;

public class UserAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("成功访问UserAddServlet");
        String username=request.getParameter("username");
        String password1=request.getParameter("password1");
        String password2=request.getParameter("password2");
     /*   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String createtime = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳*/
        Timestamp createtime=new Timestamp(System.currentTimeMillis());
        if(password1.equals(password2)){
            DBUtils db = new DBUtils();
            String condition = "username='"+username+"'";
            List<String[]> list = db.find("user",new String[]{"uid","username","password","role_id"},condition);
            if(list.isEmpty()){
                System.out.println("无相同记录");
                try { Connection conn=null;
                    PreparedStatement ptmt=null;
                    ResultSet rs=null;
                    Class.forName("com.mysql.cj.jdbc.Driver");//注册数据库驱动
                    String url = "jdbc:mysql://localhost:3306/boke?useSSL=false&serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8";
                    conn= DriverManager.getConnection(url, "root", "123456");//通过DriverManager获取数据库
                    String  sql="insert into user(username,password,role_id,createtime) values(?,?,0,?)";
                    ptmt=conn.prepareStatement(sql);
                    ptmt.setString(1, username);
                    ptmt.setString(2, password1);
                    ptmt.setTimestamp(3, createtime);
                    int i=ptmt.executeUpdate();
                    if(i!=0) {
                        System.out.println("注册成功");
                        request.getSession().setAttribute("status","1");
                        request.getSession().setAttribute("msg","注册成功");
                        RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
                        rd.forward(request,response);
                    }else {
                        System.out.println("注册失败");
                        request.getSession().setAttribute("status","0");
                        request.getSession().setAttribute("msg","注册失败");
                        RequestDispatcher rd = request.getRequestDispatcher("user_add.jsp");
                        rd.forward(request,response);
                    }
                    rs.close();
                    ptmt.close();
                    conn.close();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }else{
                System.out.println("用户已存在");
                request.getSession().setAttribute("status","0");
                request.getSession().setAttribute("msg","用户已存在");
                RequestDispatcher rd = request.getRequestDispatcher("user_add.jsp");
                rd.forward(request,response);
            }
        }else{
            System.out.println("两次输入的密码不相同");
            request.getSession().setAttribute("status","0");
            request.getSession().setAttribute("msg","两次输入的密码不相同");
            RequestDispatcher rd = request.getRequestDispatcher("user_add.jsp");
            rd.forward(request,response);
        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}